package com.zj.osm.entity.db;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.zj.osm.entity.greendao.Lay;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "LAY".
*/
public class LayDao extends AbstractDao<Lay, Long> {

    public static final String TABLENAME = "LAY";

    /**
     * Properties of entity Lay.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property LineId = new Property(1, Long.class, "lineId", false, "LINE_ID");
        public final static Property LayId = new Property(2, String.class, "layId", false, "LAY_ID");
        public final static Property LayName = new Property(3, String.class, "layName", false, "LAY_NAME");
        public final static Property LayXY = new Property(4, String.class, "layXY", false, "LAY_XY");
        public final static Property LayState = new Property(5, String.class, "layState", false, "LAY_STATE");
    }

    private Query<Lay> line_LayListQuery;

    public LayDao(DaoConfig config) {
        super(config);
    }
    
    public LayDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"LAY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"LINE_ID\" INTEGER," + // 1: lineId
                "\"LAY_ID\" TEXT," + // 2: layId
                "\"LAY_NAME\" TEXT," + // 3: layName
                "\"LAY_XY\" TEXT," + // 4: layXY
                "\"LAY_STATE\" TEXT);"); // 5: layState
        // Add Indexes
        db.execSQL("CREATE UNIQUE INDEX " + constraint + "IDX_LAY_LINE_ID ON \"LAY\"" +
                " (\"LINE_ID\" ASC);");
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"LAY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Lay entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long lineId = entity.getLineId();
        if (lineId != null) {
            stmt.bindLong(2, lineId);
        }
 
        String layId = entity.getLayId();
        if (layId != null) {
            stmt.bindString(3, layId);
        }
 
        String layName = entity.getLayName();
        if (layName != null) {
            stmt.bindString(4, layName);
        }
 
        String layXY = entity.getLayXY();
        if (layXY != null) {
            stmt.bindString(5, layXY);
        }
 
        String layState = entity.getLayState();
        if (layState != null) {
            stmt.bindString(6, layState);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Lay entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long lineId = entity.getLineId();
        if (lineId != null) {
            stmt.bindLong(2, lineId);
        }
 
        String layId = entity.getLayId();
        if (layId != null) {
            stmt.bindString(3, layId);
        }
 
        String layName = entity.getLayName();
        if (layName != null) {
            stmt.bindString(4, layName);
        }
 
        String layXY = entity.getLayXY();
        if (layXY != null) {
            stmt.bindString(5, layXY);
        }
 
        String layState = entity.getLayState();
        if (layState != null) {
            stmt.bindString(6, layState);
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Lay readEntity(Cursor cursor, int offset) {
        Lay entity = new Lay( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // lineId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // layId
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // layName
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // layXY
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5) // layState
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Lay entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setLineId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setLayId(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setLayName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setLayXY(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setLayState(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Lay entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Lay entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Lay entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "layList" to-many relationship of Line. */
    public List<Lay> _queryLine_LayList(Long lineId) {
        synchronized (this) {
            if (line_LayListQuery == null) {
                QueryBuilder<Lay> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.LineId.eq(null));
                line_LayListQuery = queryBuilder.build();
            }
        }
        Query<Lay> query = line_LayListQuery.forCurrentThread();
        query.setParameter(0, lineId);
        return query.list();
    }

}
